package service.impl;

import dao.OrdersDao;
import dao.impl.OrdersDaoImpl;
import service.OrdersService;
import urity.Page;
import vo.OrdersVo;

import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    OrdersDao dao = new OrdersDaoImpl();

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param sql
     * @return
     */
    @Override
    public List<OrdersVo> selectAll(int pageNo, int pageSize, String sql) {
        return dao.selectAll(pageNo, pageSize, sql);
    }

    /**
     * 查询总记录数
     *
     * @param sql
     * @return
     */
    @Override
    public int selectCount(String sql) {
        return dao.selectCount(sql);
    }

    /**
     * @param pageNo
     * @param pageSize
     * @param orderNumder 订单号
     * @param stateID     状态
     * @return
     */
    @Override
    public Page selectLimit(int pageNo, int pageSize, String orderNumder, int stateID) {
        if(pageNo<=0){
            pageNo=1;
        }
        //拼接sql语句
        String sql2="select count(0) from orders a join site b on b.siteID=a.siteID join order_status c on c.stateID=a.stateID join distribution d on d.id=a.distribution  where c.stateID>2 ";
        String sql = "select a.orderID,a.order_numder,b.consigneeName,b.consigneephoone,b.site,a.upmoney,d.d_name,a.payment_time,a.creation_time,c.stateName,a.remark,a.delivery_time  from orders a join site b on b.siteID=a.siteID join order_status c on c.stateID=a.stateID join distribution d on d.id=a.distribution where c.stateID>2  ";
        if (orderNumder != null && !orderNumder.equals("")) {
            sql2+=" and a.order_numder='" + orderNumder + "'";
            sql += " and a.order_numder='" + orderNumder + "'";
        }
        if (stateID != 0 && stateID > 2) {
            sql2+="  and c.stateID='" + stateID + "'";
            sql += "  and c.stateID='" + stateID + "'";
        }
        Page page = new Page();
        //总记录数
        int countPage = selectCount(sql2);

        page.setCountPage(countPage);

        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        //总页数
        int totalPage = 0;
        if (countPage % pageSize == 0) {
            totalPage = countPage / pageSize;
        } else {
            totalPage = countPage / pageSize + 1;
        }
        page.setTotalPage(totalPage);
        //继续拼接分页
        sql += " LIMIT  ?, ?";
        List<OrdersVo> list = selectAll(pageNo, pageSize, sql);
        page.setList(list);
        return page;
    }

    /**
     * 根据id查询OrdersVo数据
     *
     * @param id
     * @return
     */
    @Override
    public OrdersVo selectOne(int id) {
        return dao.selectOne(id);
    }

    /**
     * 修改发货时间
     * @param id
     * @return
     */
    @Override
    public int update(int id) {
        return dao.update(id);
    }
}
